package com.api.syh56.shengyunhui.service.impl;

import com.api.syh56.shengyunhui.entity.adminEntity.AdminUser;
import com.api.syh56.shengyunhui.entity.adminEntity.Permission;
import com.api.syh56.shengyunhui.exception.BaseException;
import com.api.syh56.shengyunhui.mapper.AdminSupMapper;
import com.api.syh56.shengyunhui.mapper.PermissionMapper;
import com.api.syh56.shengyunhui.service.AdminSupService;
import com.api.syh56.shengyunhui.util.JWTUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
public class AdminSupServiceImpl extends ServiceImpl<AdminSupMapper,AdminUser> implements AdminSupService {
    @Autowired
    private AdminSupMapper adminSupMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
//    public String login(AdminUser adminUser) {
    public HashMap login(AdminUser adminUser) {
        String token = "";
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        try {
            String md5 = "";
            md5 = DigestUtils.md5DigestAsHex(adminUser.getPassword().getBytes("utf-8"));
            QueryWrapper<AdminUser> wrapper = new QueryWrapper<>();
            wrapper.eq("username", adminUser.getUsername());
            wrapper.eq("password",md5);
            AdminUser admin = adminSupMapper.selectOne(wrapper);

            if (admin != null) {

                HashMap<String, String> adminLog = new HashMap<>();
                adminLog.put("id",admin.getId().toString());
                adminLog.put("username",admin.getUsername());
                token = JWTUtils.getToken(adminLog);
                stringObjectHashMap.put("token",token);
                stringObjectHashMap.put("role",admin.getRole());
                stringObjectHashMap.put("userName",admin.getUsername());
                return stringObjectHashMap;
            }else {
                QueryWrapper<AdminUser> wrapperUserName = new QueryWrapper<>();
                wrapperUserName.eq("username", adminUser.getUsername());
                AdminUser userName = adminSupMapper.selectOne(wrapperUserName);
                if (userName == null) {
                    throw new BaseException("用户名不存在");
                }else {
                    if (!userName.getPassword().equals(adminUser.getPassword())) {
                        throw new BaseException("密码错误");
                    }
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return stringObjectHashMap;
    }

    @Override
    public void reg(AdminUser adminUser) {
        try{
            String md5 = "";
            md5 = DigestUtils.md5DigestAsHex(adminUser.getPassword().getBytes("utf-8"));
            adminUser.setPassword(md5);
            adminSupMapper.insert(adminUser);
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
    }

    @Override
    public Page<AdminUser> get(Integer pageNum,Integer pageSize) {
        Page<AdminUser> page = new Page<>(pageNum, pageSize);
       Page<AdminUser> adminUsers = adminSupMapper.selectPage(page,null);
        return adminUsers;
    }

    @Override
    public void regPer(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public List<Permission> getPer() {
        List<Permission> permissions = permissionMapper.selectList(null);
        return permissions;
    }

    @Override
    public void addJobNum(AdminUser adminUser) {
        adminSupMapper.insert(adminUser);
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateById(permission);
    }

}
